package com.csxy.six.controller;
import com.csxy.six.R;
import com.csxy.six.entity.Category;
import com.csxy.six.service.CategoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 分类控制器
 * 提供分类相关的RESTful API
 */
@RestController
@RequestMapping("/annoservice/category")
@Tag(name = "分类管理", description = "提供公告分类的增删改查接口")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @Operation(summary = "获取所有分类")
    @GetMapping
    public R getAll() {
        return R.ok().data("list", categoryService.list());
    }

    @Operation(summary = "根据ID获取分类")
    @GetMapping("/{id}")
    public R getById(@PathVariable Integer id) {
        return R.ok().data("category", categoryService.getById(id));
    }

    @Operation(summary = "新增分类")
    @PostMapping
    public R save(@RequestBody Category category) {
        return categoryService.save(category) ? R.ok() : R.error();
    }

    @Operation(summary = "修改分类")
    @PutMapping
    public R update(@RequestBody Category category) {
        return categoryService.updateById(category) ? R.ok() : R.error();
    }

    @Operation(summary = "删除分类")
    @DeleteMapping("/{id}")
    public R remove(@PathVariable Integer id) {
        return categoryService.removeById(id) ? R.ok() : R.error();
    }
}